﻿using System;
using System.Collections.Generic;
using System.Text;

namespace cici
{
    class Mvc : CICIMysql
    {
        private Mvccontroller mvccontroller = new Mvccontroller();
        private Mvcview mvcview = new Mvcview();
        private Mvcmodelcrud mvcmodelcrud = new Mvcmodelcrud();

        public Mvc()
        {
            Console.WriteLine("\nPlease wait, connection database...");
            if (!this.testConnection())
                Console.WriteLine("\nConnection to database failed");
        }

        public Boolean createMvc(string className)
        {
            Boolean status = false;

            if (className.Equals("000;"))
            {
                status = this.createMvcFromTable(null);
            }
            else
            {
                string[] temps = className.Split(';');
                foreach (string tempTableName in temps)
                {
                    if (tempTableName.Length > 0)
                    {
                        status = this.createMvcFromTable(tempTableName);
                    }
                }
            }

            return status;
        }

        private Boolean createMvcFromTable(string tableRequest)
        {
            Boolean status = false;

            List<object> tables = new List<object>();

            if (tableRequest==null)
            {
                tables = this.getAllTables();
            }
            else
            {
                tables = this.getAnyTables(tableRequest);
            }

            string templateFooter = "";

            if(tables.Count > 0)
            {
                foreach (object table in tables)
                {
                    Dictionary<object, object> columns = this.getAllColumnsFromTable(table.ToString());

                    mvccontroller.forColumns = columns;
                    mvccontroller.createMvccontroller(table.ToString());

                    mvcview.forColumns = columns;
                    mvcview.createAll(table.ToString());

                    mvcmodelcrud.createMvcmodelcrud(table.ToString());

                    templateFooter += "<li><a href=\"<?php echo base_url('/" +
                            table.ToString() + "');?>\">" +
                            table.ToString() + "</a></li>";
                }

                mvcview.createHeaderFooter(templateFooter);

                status = true;
            }

            return status;

        }
    }
}
